package com.ysh.web.controller.store;



// 访问路径：uri:/store/company?operation = list

import com.github.pagehelper.PageInfo;
import com.ysh.domain.store.Company;
import com.ysh.service.store.CompanyService;
import com.ysh.service.store.impl.CompanyServiceImpl;
import com.ysh.utils.BeanUtil;
import com.ysh.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/store/company/*")
public class CompanyServlet extends BaseServlet {

    CompanyService companyService = new CompanyServiceImpl();

    /*
        删除操作
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取前台id
        String sid = req.getParameter("id");
        // 将数据获取到，封装成一个对象
        // Company company = BeanUtil.fillBean(req,Company.class);
        // 调用业务层接口
        companyService.deleteByIds(sid);

        // 跳转回页面list
        // list(req,resp);
        resp.sendRedirect(req.getContextPath()+"/store/company/list");


    }


    /*
        修改操作
     */
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将数据获取到，封装成一个对象
        Company company = BeanUtil.fillBean(req,Company.class,"yyyy-MM-dd");
        // 调用业务层接口
        // CompanyService companyService = new CompanyServiceImpl();
        companyService.update(company);

        // 跳转回页面list
        // list(req,resp);
        resp.sendRedirect(req.getContextPath()+"/store/company/list");
    }

    /*
        跳转到修改页面,并带入数据
        根据id查询信息，并将结果带入页面中进行回显
     */
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 查询要修改的数据findById
        String id = req.getParameter("id");
        // CompanyService companyService = new CompanyServiceImpl();
        // 根据id查询结果
        Company company = companyService.findById(id);
        // 将数据加载到指定区域，供页面获取
        req.setAttribute("company",company);
        // 存储转发到update.jsp页面
        req.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(req,resp);
    }

    /*
        添加操作
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 将数据获取到，封装成一个对象
        Company company = BeanUtil.fillBean(req,Company.class,"yyyy-MM-dd");
        // 调用业务层接口
        // CompanyService companyService = new CompanyServiceImpl();
        companyService.save(company);
        // 跳转回页面list
        // list(req,resp);
        resp.sendRedirect(req.getContextPath()+"/store/company/list");
    }

    /*
        跳转到添加页面
     */
    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 跳转页面
        // D:\IdeaProjects\web_maven\mm\src\main\webapp\WEB-INF\pages\store\company\add.jsp
        req.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(req,resp);

    }

    /*
        查询所有
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 进入列表页
        // 获取数据
        // 默认设置，当前页1
        int page = 1;
        // 默认设置，每页条数10
        int size = 20;
        // 判断页面是否获取值，有值，按页面参数进行分页
        // 当前页面
        if (StringUtils.isNoneBlank(req.getParameter("page"))){
            page = Integer.parseInt(req.getParameter("page"));
        }
        // 每页显示数据条数
        if (StringUtils.isNoneBlank(req.getParameter("size"))){
            page = Integer.parseInt(req.getParameter("size"));
        }
        // 指定参数进行分页查询
        PageInfo all = companyService.findAll(page, size);
        // 将数据保存到指定的位置
        req.setAttribute("page",all);
        // 跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(req,resp);
    }

}
